
const int numRows = 4;
const int numCols = 4;
const int debounceTime = 20;
const char keymap[numRows][numCols] = {
  {'1','2','3','A'},
  {'4','5','6','B'},
  {'7','8','9','C'},
  {'*','0','#','D'},
};
const int rowPins[numRows] = {4,5,6,7};
const int colPins[numCols] = {8,9,10,11};

void setup() {
  // put your setup code here, to run once:

  Serial.begin(9600);
  
  for (int rows = 0; rows < numRows; rows++){
    pinMode(rowPins[rows],INPUT);
    digitalWrite(rowPins[rows],HIGH);
  }
  
  for (int colnum = 0; colnum < numCols; colnum++){
    pinMode(colPins[colnum],OUTPUT);
    digitalWrite(colPins[colnum],HIGH);
  }
  
  
}

void loop() {
  // put your main code here, to run repeatedly:
  char key = theValues();
  
  if(key != 0){
    Serial.println(key);
  }
}

char theValues(){
  char key = 0;
  for (int col = 0; col < numCols; col ++){
    digitalWrite(colPins[col],LOW);
    
    for (int row = 0; row < numRows; row ++){
      
      if (digitalRead(rowPins[row]) == LOW){
        delay(debounceTime);
        while(digitalRead(rowPins[row]) == LOW);
        key = keymap[row][col];
      }
    }
    digitalWrite(colPins[col],HIGH);
  }

  return key;
}


